
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*; 
import java.io.*;
import java.util.*;


public class XmlFrame extends JPanel{

	/**
	 * 
	 */
	private static final long serialVersionUID = -802300231134699814L;
	String compact;
	String content;
	
	JTextArea compactPolicy;
	JTextArea policy;
	
	PolicyFrame parent;
	
	public XmlFrame(){
		super(new FlowLayout(FlowLayout.CENTER));
		compact="";
		content="";
		parent=null;
	}
	
	public XmlFrame(PolicyFrame p){
		super(new FlowLayout(FlowLayout.CENTER));
		compact="";
		content="";
		parent=p;
	}
	
	public void setParent(PolicyFrame p){
		parent=p;
	}
	
	public void config(){
		compactPolicy=new JTextArea(1,60);
		policy=new JTextArea(34,60);
		compactPolicy.setEditable(false);
		policy.setEditable(false);
//		policy.setLineWrap(true);
		JScrollPane sp=new JScrollPane(policy);
		sp.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		sp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		JLabel cLabel=new JLabel("Compact Policy");
		JLabel pLabel=new JLabel("Complete XML policy");
		
		JPanel p1=new JPanel(new FlowLayout(FlowLayout.LEFT));
		JPanel p2=new JPanel(new FlowLayout(FlowLayout.LEFT));
		p1.add(cLabel);
		p2.add(pLabel);
		JPanel p3=new JPanel(new BorderLayout());
		JPanel p4=new JPanel(new BorderLayout());
		JPanel p5=new JPanel(new BorderLayout());
		p3.add(p1,"North");
		p3.add(compactPolicy,"Center");
		p4.add(p3,"North");
		p4.add(p2,"Center");
		p5.add(p4,"North");
		p5.add(sp,"Center");
		
		add(p5);
	}
	
	public void refresh(){
		parent.flushData();
		constructCP();
		constructP();
		compactPolicy.setText(compact);
		policy.setText(content);
	}
	
	public void constructCP(){
		
	}
	
	public void constructP(){
		PolicyProperty pp=parent.property.pp;
		content="";
		addExpire(pp);
		addPolicyInfo(pp);
		addDataAccessInformation(pp);
		addDispute(pp);
		
		addData();
		
		addBody();
	}
	
	// adds expire information
	public void addExpire(PolicyProperty pp){
		content=content+"  <!-- Expiry information for this policy -->\n";
		if(pp.calMode){
			//by lifetime
			int time=pp.lifeTime;
			if(pp.unit.equals("Minute(s)")){
				time=time*60;
			}
			if(pp.unit.equals("Hour(s)")){
				time=time*3600;
			}
			if(pp.unit.equals("Day(s)")){
				time=time*86400;
			}
			if(pp.unit.equals("Week(s)")){
				time=time*604800;
			}
			content=content+"  <EXPIRY max-age=\""+time+"\"/>\n\n";
		}else{
			//by date
			int d=parent.property.exD.getSelectedIndex()+1;			
			int m=parent.property.exM.getSelectedIndex();
			int y=Integer.valueOf(pp.expireYear).intValue()-1900;
			Date date=new Date(y,m,d);
			
			String[] weekDays={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"}; 
		    Calendar cal=Calendar.getInstance(); 
		    cal.setTime(date); 
		    int w=cal.get(Calendar.DAY_OF_WEEK)-1; 
		    if(w<0)w=0;
		    String time=weekDays[w];
		    time=time+", "+pp.expireDay+" "+pp.expireMonth+" "+pp.expireYear+" 12:00:00 GMT";
		    content=content+"  <EXPIRY date=\""+time+"\"/>\n\n";
		}
	}
	
	//add policy information
	public void addPolicyInfo(PolicyProperty pp){
		content=content+"<POLICY\n";
		if(!(pp.policyName.replaceAll(" ", "")).equals("")){
			//add policy name
			content=content+"  name=\""+pp.policyName+"\"\n";
		}
		if(!(pp.readablePolicyUrl.replaceAll(" ", "")).equals("")){
			//add human-readable policy
			String url=pp.readablePolicyUrl;
			if(!url.contains("//"))
				url="http://"+url;
			content=content+"  discuri=\""+url+"\"\n";
		}
		if(!(pp.optUrl.replaceAll(" ", "")).equals("")){
			//add opt-in/out url
			String optUrl=pp.optUrl;
			if(!optUrl.contains("//"))
				optUrl="http://"+optUrl;
			content=content+"  opturi=\""+optUrl+"\"\n";
		}
		String lang="";
		String l=pp.policyLanguage;
		if(l.equals("Chinese")){
			lang="zh";
		}
		if(l.equals("English")){
			lang="en";
		}
		if(l.equals("French")){
			lang="fr";
		}
		if(l.equals("German")){
			lang="de";
		}
		if(l.equals("Greek")){
			lang="el";
		}
		if(l.equals("Italian")){
			lang="it";
		}
		if(l.equals("Japanese")){
			lang="jp";
		}
		if(l.equals("Klingon")){
			lang="x-klingon";
		}
		if(l.equals("Korean")){
			lang="ko";
		}
		if(l.equals("Portugese")){
			lang="pt";
		}
		if(l.equals("Russian")){
			lang="ru";
		}
		if(l.equals("Spanish")){
			lang="es";
		}
		//set language
		content=content+"  xml:lang=\""+lang+"\">\n";
		//add test only tag
		if(pp.isForTest()){
			content=content+"  <!-- Test-only policy - not for actual use -->\n  <TEST/>\n";
		}
		
		//add company info
		content=content+"\n  <!-- Description of the entity making this policy statement. -->\n";
		content=content+"  <ENTITY>\n";
		content=content+"  <DATA-GROUP>\n";
		if(!pp.organizationName.equals("")){
			//add organization name
			content=content+"<DATA ref=\"#business.name\">"+pp.organizationName+"</DATA>\n";
		}
		
		if(!pp.emailAddress.equals("")){
			//add email address
			content=content+"<DATA ref=\"#business.contact-info.online.email\">"+pp.emailAddress+"</DATA>\n";
		}
		
		if(!pp.webHomePage.equals("")){
			//add homepage
			if(pp.webHomePage.length()<7||!pp.webHomePage.substring(0,7).equals("http://")){
				pp.webHomePage="http://"+pp.webHomePage;
				parent.property.webpage.setText(pp.webHomePage);
			}
			content=content+"<DATA ref=\"#business.contact-info.online.uri\">"+pp.webHomePage+"</DATA>\n";
		}
		
		if(!pp.phoneNumber.equals("")){
			//add phone number
			content=content+"<DATA ref=\"#business.contact-info.telecom.telephone.number\">"+pp.phoneNumber+"</DATA>\n";
		}
		
		if(!pp.addressName.equals("")){
			//add address name
			content=content+"<DATA ref=\"#business.contact-info.postal.organization\">"+pp.addressName+"</DATA>\n";
		}
		
		if(!pp.address.equals("")){
			//add street
			content=content+"<DATA ref=\"#business.contact-info.postal.street\">"+pp.address+"</DATA>\n";
		}
		
		if(!pp.city.equals("")){
			//add city
			content=content+"<DATA ref=\"#business.contact-info.postal.city\">"+pp.city+"</DATA>\n";
		}
	    
		if(!pp.state.equals("")){
			//add state/province
			content=content+"<DATA ref=\"#business.contact-info.postal.stateprov\">"+pp.state+"</DATA>\n";
		}
		
		if(!pp.postalCode.equals("")){
			//add zipcode
			content=content+"<DATA ref=\"#business.contact-info.postal.postalcode\">"+pp.postalCode+"</DATA>\n";
		}
		
		if(!pp.country.equals("")){
			//add country
			content=content+"<DATA ref=\"#business.contact-info.postal.country\">"+pp.country+"</DATA>\n";
		}
		content=content+"  </DATA-GROUP>\n  </ENTITY>\n";	
	}
	
	//add data access policy
	public void addDataAccessInformation(PolicyProperty pp){
		content=content+"\n  <!-- Disclosure -->\n";
		String ac="";
		switch(pp.access){
		case 0:
			ac="<nonident/>";
			break;
		case 1:
			ac="<all/>";
			break;
		case 2:
			ac="<contact-and-other/>";
			break;
		case 3:
			ac="<ident-contact/>";
			break;
		case 4:
			ac="<other-ident/>";
			break;
		case 5:
			ac="<none/>";
			break;
		default:
			break;
		}
		content=content+"  <ACCESS>"+ac+"</ACCESS>\n";  
	}
	
	//add dispute information
	public void addDispute(PolicyProperty pp){
		if(pp.assurance.size()<=0){
			content = content+"\n  <!-- No dispute information -->\n";
		}else{
			content= content+"\n  <!-- Disputes -->\n  <DISPUTES-GROUP>\n";
			for(Iterator<Assurance> it=pp.assurance.iterator();it.hasNext();){
				content=content+it.next().toXml();
			}
			content=content+"  </DISPUTES-GROUP>\n";
		}
	}
	
	public void addBody(){
		content="  <!-- Generated by Yixiao's P3P editor -->\n\n"+content;
		content="<POLICIES xmlns=\"http://www.w3.org/2002/01/P3Pv1\">\n\n"+content;
		content="<?xml version=\"1.0\"?>\n"+content;
		
		content=content+"\n<!-- End of policy -->\n";
		content=content+"</POLICY>\n";
		content=content+"</POLICIES>\n";
		
	}
	
	public void addData(){
		NutritionFrame n=parent.nutrition;
		for(int i=0;i<n.tables.size();i++){
			
			NutritionTableView view=n.tables.get(i);
			content=content+"\n  <!-- Statement for table \""+view.name+"\" -->\n";
			
			for(int row=1;row<11;row++){
				
				boolean rowempty=true;
				for(int col=2;col<9;col++){
					if(col==6){
					}else{
						if(!(((ImageIcon)view.data[row][col]).getDescription()).equals("0")){
							rowempty=false;
							break;
						}
					}
				}
				if(rowempty){continue;}
				content=content+"  <STATEMENT>\n";
				
				if(view.description.equals("")){
					content=content+"\n    <!-- No consequence specified -->\n";
				}else{
					content=content+"\n    <CONSEQUENCE>"+view.description+"</CONSEQUENCE>\n";
				}
				
				if(view.identifiable){
					content=content+"\n    <!-- Data in this statement is marked as being non-identifiable -->\n" +
							"    <NON-IDENTIFIABLE/>\n";
				}
				
				//purpose
				content=content+"\n    <!-- Use (purpose) -->\n    <PURPOSE>\n";
				boolean notempty=false;
				for(int col=2;col<9;col++){
					if(col==6){
						continue;
					}else{
						if((((ImageIcon)view.data[row][col]).getDescription()).equals("0")){
							continue;
						}else{
							
							int opt=0;
							if((((ImageIcon)view.data[row][col]).getDescription()).equals("2"))
								opt=1;
							if((((ImageIcon)view.data[row][col]).getDescription()).equals("3"))
								opt=2;
								
							Detail d=view.detail[row][col];
							
							for(int ii=16;ii<=26;ii++){
								if(d.isTrue(ii)){
									notempty=true;
									content=content+"      <";
									switch(ii){
										case 16:
											content=content+"current";
											break;
										case 17:
											content=content+"admin";
											break;
										case 18:
											content=content+"develop";
											break;
										case 19:
											content=content+"tailoring";
											break;
										case 20:
											content=content+"historical";
											break;
										case 21:
											content=content+"contact";
											break;
										case 22:
											content=content+"telemarketing";
											break;
										case 23:
											content=content+"pseudo-analysis";
											break;
										case 24:
											content=content+"pseudo-decision";
											break;
										case 25:
											content=content+"individual-analysis";
											break;
										case 26:
											content=content+"individual-decision";
											break;
										default:
											break;
									}
									if(opt==1){
										content=content+" required=\"opt-in\"";
									}
									if(opt==2){
										content=content+" required=\"opt-out\"";
									}
									content=content+"/>\n";
								}
							}
						}
					}
				}
				if(!notempty){
					content=content+"      <!-- No purpose specified -->\n";
				}
				content=content+"    </PURPOSE>\n";
				
				
				//recipient
				content=content+"\n    <!-- Recipients -->\n    <RECIPIENT>\n";
				notempty=false;
				for(int col=2;col<9;col++){
					if(col==6){
						continue;
					}else{
						if((((ImageIcon)view.data[row][col]).getDescription()).equals("0")){
							continue;
						}else{
							
							int opt=0;
							if((((ImageIcon)view.data[row][col]).getDescription()).equals("2"))
								opt=1;
							if((((ImageIcon)view.data[row][col]).getDescription()).equals("3"))
								opt=2;
								
							Detail d=view.detail[row][col];

							for(int ii=27;ii<=32;ii++){
								if(d.isTrue(ii)){
									notempty=true;
									content=content+"      <";
									switch(ii){
										case 27:
											content=content+"ours";
											break;
										case 28:
											content=content+"delivery";
											break;
										case 29:
											content=content+"same";
											break;
										case 30:
											content=content+"other-recipient";
											break;
										case 31:
											content=content+"unrelated";
											break;
										case 32:
											content=content+"public";
											break;
										default:
											break;
									}
									if(opt==1){
										content=content+" required=\"opt-in\"";
									}
									if(opt==2){
										content=content+" required=\"opt-out\"";
									}
									content=content+"/>\n";
								}
							}
						}
					}
				}
				if(!notempty){
					content=content+"      <!-- No recipient specified -->\n";
				}
				content=content+"    </RECIPIENT>\n";
				
				//retention
				content=content+"\n    <!-- Retention -->\n    <RETENTION>";
				switch(view.retention){
				case 0:
					content=content+"<no-retention/>";
					break;
				case 1:
					content=content+"<stated-purpose/>";
					break;
				case 2:
					content=content+"<legal-requirement/>";
					break;
				case 3:
					content=content+"<business-practices/>";
					break;
				case 4:
					content=content+"<indefinitely/>";
					break;
				default:
					break;
				}
				content=content+"</RETENTION>\n";
				
				content=content+"\n    <DATA-GROUP>\n";
				for(int col=2;col<9;col++){
					if(col==6){
						continue;
					}else{
						if((((ImageIcon)view.data[row][col]).getDescription()).equals("0")){
							continue;
						}else{
							
							int opt=0;
							if((((ImageIcon)view.data[row][col]).getDescription()).equals("2"))
								opt=1;
							if((((ImageIcon)view.data[row][col]).getDescription()).equals("3"))
								opt=2;
								
							Detail d=view.detail[row][col];
							
							content=content+"      <DATA ref=\"#dynamic.miscdata\"";
							if(opt!=0){
								content=content+" optional=\"yes\"";
							}
							content=content+">\n";
							
							content=content+"        <CATEGORIES>";
							
							if(d.isTrue(0)){
								content=content+"<physical/>";
							}
							if(d.isTrue(1)){
								content=content+"<online/>";
							}
							if(d.isTrue(2)){
								content=content+"<state/>";
							}
							if(d.isTrue(3)){
								content=content+"<uniqueid/>";
							}
							if(d.isTrue(4)){
								content=content+"<demographic/>";
							}
							if(d.isTrue(5)){
								content=content+"<financial/>";
							}
							if(d.isTrue(6)){
								content=content+"<health/>";
							}
							if(d.isTrue(7)){
								content=content+"<preference/>";
							}
							if(d.isTrue(8)){
								content=content+"<purchase/>";
							}
							if(d.isTrue(9)){
								content=content+"<government/>";
							}
							if(d.isTrue(10)){
								content=content+"<political/>";
							}
							if(d.isTrue(11)){
								content=content+"<interactive/>";
							}
							if(d.isTrue(12)){
								content=content+"<content/>";
							}
							if(d.isTrue(13)){
								content=content+"<computer/>";
							}
							if(d.isTrue(14)){
								content=content+"<navigation/>";
							}
							if(d.isTrue(15)){
								content=content+"<location/>";
							}
							content=content+"</CATEGORIES>\n";
							content=content+"      </DATA>\n";
						}
					}
				}
				content=content+"    </DATA-GROUP>\n";
				content=content+"\n  </STATEMENT>\n\n\n";
			}
		}
	}
	
}
